import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86131
 * Date: 2024-01-30
 * Time: 12:55
 */

//1.定义一个方法,存入三个对象
    //属性为:id,username,paaword
//2.要求:定义一个方法,根据id查找对应的用户信息,如果存在,返回索引,不存在返回-1
public class Test4 {
    public static void main(String[] args) {
        //1.创建集合
        ArrayList<User> list = new ArrayList<>();

        //2.创建三个用户对象
        User u1 = new User("heima001","zhangsan","123456");
        User u2 = new User("heima002","lisi","123456");
        User u3 = new User("heima003","wangwu","123456");

        //3.把用户对象添加到集合中
        list.add(u1);
        list.add(u2);
        list.add(u3);

        //4.查找id
        int index = getIndex(list, "heima002");
        System.out.println(index);
    }

    public static int getIndex(ArrayList<User> list,String id) {
        for (int i = 0; i < list.size(); i++) {
            User u = list.get(i);
            String uId = u.getId();
            if(uId.equals(id)) {
                return i;
            }
        }
        return -1;
    }
}
